import QtQuick 2.0
import QtQuick.Controls 2.2

Item {
    id:sdlogpage
    width: 400
    height: 400

    property ListModel sdOnlineModel: ListModel{}
    property ListModel sdOfflineModel: ListModel{}
    property ListModel sdEmgModel: ListModel{
//        ListElement{
//            errorDevType:7
//            errorStr:"Open"
//            errorAddr:"Test Addr "
//            datetime: new Date().toLocaleString(Qt.locale() , "yyyy-MM-dd hh:mm:ss")

//        }
//        ListElement{
//            errorDevType:1
//            errorStr:"Open"
//            errorAddr:"Test Addr "
//            datetime: new Date().toLocaleString(Qt.locale() , "yyyy-MM-dd hh:mm:ss")
//        }
    }

    function sdEvent(error , addr , devType , type){
        var model = sdOnlineModel
//        switch(type){
//        case "Online":
//            model = sdOnlineModel
//            break
//        case "Offline":
//             model = sdOfflineModel
//            break
//        case "Emg":
//             model = sdEmgModel
//            break
//        default:
//            return
//        }

        model.insert(0 , {"errorDevType":devType ,
                         "errorStr":error ,
                         "errorAddr":addr ,
                         "datetime":new Date().toLocaleString(Qt.locale() , "yyyy-MM-dd hh:mm:ss")
                       })
    }





    PieChartView{
        id:devpiechart
        width: parent.width
        height: parent.height*0.5
    }

    Timer{
        id:checSDWorkState
        interval: 5000
        repeat: true
        running: true
        onTriggered: {
            var emgcnt , offlinecnt , onlinecnt
            var cntinfo = SDController.getSDSateInfo()
            console.log(cntinfo.split("*"))
            emgcnt = parseInt(cntinfo.split("*")[0])
            offlinecnt = parseInt(cntinfo.split("*")[1])
            onlinecnt = parseInt(cntinfo.split("*")[2])

            devpiechart.setDevCntInfo(emgcnt , offlinecnt , onlinecnt)
        }
    }

    ListView {
        id: sdOnlinelistView
        width: parent.width
        height: parent.height*0.5-40
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: devpiechart.bottom
        anchors.topMargin: 0
        clip: true
        delegate: logdelegate
        model: sdOnlineModel
        Rectangle{anchors.fill: parent ; color: "#55fc51" ; opacity: 0.7; z:-1}
    }

    Button{
        id:clearRecord
        text: "清除记录"
        anchors.top: sdOnlinelistView.bottom
        anchors.left: parent.left
        anchors.leftMargin: 0
        onClicked: {
            sdOnlineModel.clear()
        }
    }


    Component{
        id:logdelegate
        Item {
            width: parent.width
            height: 40

            Rectangle {
                id: rectangle
                color: Qt.rgba(1,1,1,0.5)
                anchors.fill: parent
                radius: 5

                Text {
                    id: txtErrorTime
                    x: 16
                    y: 10
                    width: 203
                    height: 40
                    text: datetime
                    font.pixelSize: 22
                }

                Text {
                    id: txtErrorDevType
                    x: 260
                    y: 10
                    width: 184
                    height: 40
                    text: "设备类型:" + errorDevType
                    font.pixelSize: 22
                }

                Text {
                    id: text1
                    x: 600
                    y: 0
                    width: 800
                    height: 40
                    text: errorAddr+"   "+errorStr
                    verticalAlignment: Text.AlignVCenter
                    clip: true
                    wrapMode: Text.WordWrap
//                    horizontalAlignment: Text.AlignHCenter
                    font.pixelSize: 30
                }
            }
        }
    }


}
